//
//  LBGreyscaleFilter.mm
//  OpenGLGame
//
//  Created by Nguyen Cong Huy on 11/22/12.
//  Copyright (c) 2012 UIT. All rights reserved.
//

#include <iostream>
#include "LBGreyscaleFinalFilter.h"

char fShaderStrGreyscaleFinal[] = ""
"precision mediump float;"

"varying vec2 vtexCoor;"
"varying vec4 vposition;"
"uniform sampler2D texture;"

"varying highp vec2 vtexCoor2;"
"uniform sampler2D texture2; "

"const highp vec3 W = vec3(0.2125, 0.7154, 0.0721);"

//contrast function (default 1.0)
"vec4 contrastColor(vec4 color, float contrast){"
"color.r = (color.r - 0.5)*contrast + 0.5;"
"color.g = (color.g - 0.5)*contrast + 0.5;"
"color.b = (color.b - 0.5)*contrast + 0.5;"
"color = clamp(color, 0.0, 1.0);"
"return color;"
"}"
//END contrast function

//brightness function (default 0.0)
"vec4 brightnessColor(vec4 color, float brightness){"
"color.r = color.r + brightness;"
"color.g = color.g + brightness;"
"color.b = color.b + brightness;"
"color = clamp(color, 0.0, 1.0);"
"return color;"
"}"
//END brightness function

// radial gradient color function
"vec4 radialGradientColor(vec4 centerColor, vec4 outerColor, vec2 myPosition, vec2 centerPosition){"
"    float distance = clamp(distance(myPosition, centerPosition), 0.0, 1.0);"
"    return mix(centerColor, outerColor, distance);"
"}"
// radial gradient color function

// hard light blend function
"vec4 hardLight(vec4 base, vec4 blend, float opacity){"
"   vec4 result =  base;"
"    if (blend.r > 0.5) result.r = 1.0 - (1.0-base.r) *(1.0-2.0*(blend.r-0.5));"
"    else result.r = base.r * (2.0*blend.r);"

"    if (blend.g > 0.5) result.g = 1.0 - (1.0-base.g) *(1.0-2.0*(blend.g-0.5));"
"    else result.g = base.g * (2.0*blend.g);"

"    if (blend.b > 0.5) result.b = 1.0 - (1.0-base.b) *(1.0-2.0*(blend.b-0.5));"
"    else result.b = base.b * (2.0*blend.b);"

"    result = mix(base, result, opacity);"
"    return result;"
"}"
// END hard light blend function

// overlay function
"vec4 overlay(vec4 base, vec4 blend, float opacity){"
"    vec4 result =  base;"

"   if (base.r > 0.5) result.r = 1.0 - (1.0-2.0 * (base.r - 0.5)) * (1.0-blend.r); "
"   else result.r = (2.0 * base.r) * blend.r; "

"   if (base.g > 0.5) result.g = 1.0 - (1.0-2.0 * (base.g - 0.5)) * (1.0-blend.g); "
"   else result.g = (2.0 * base.g) * blend.g; "

"   if (base.b > 0.5) result.b = 1.0 - (1.0-2.0 * (base.b - 0.5)) * (1.0-blend.b); "
"   else result.b = (2.0 * base.b) * blend.b; "

"   result = mix(base, result, opacity);"

"   return result;"

"}"
// END overlay function

"void main()"
"{"
"    lowp vec4 textureColor = texture2D(texture, vtexCoor);"
"   lowp vec4 texture2Color = texture2D(texture2, vtexCoor2);"
"    float luminance = dot(textureColor.rgb, W);"
"    vec4 result = vec4(vec3(luminance), textureColor.a);"
"   result = hardLight(result, texture2Color, 0.7);"
"    result = contrastColor(result, 1.1);"
"    result = brightnessColor(result, 0.15);"

    
"    gl_FragColor = result;"
"}";

LBGreyscaleFinalFilter::LBGreyscaleFinalFilter(){
    init(0, fShaderStrGreyscaleFinal);
}